from typing import Annotated
from sqlmodel import create_engine, SQLModel, Session
from fastapi import Depends

# 替换为你的MySQL数据库信息
DATABASE_URL = "mysql+pymysql://root:root@localhost:3306/fastapi_db"

# 创建引擎，echo=True表示在控制台输出执行的SQL语句，便于调试
engine = create_engine(DATABASE_URL, echo=True)


def create_db_and_tables():
    """创建所有定义的表"""
    print("创建数据库表...")
    SQLModel.metadata.create_all(engine)


def get_session():
    """获取数据库会话"""
    with Session(engine) as session:
        yield session


SessionDep = Annotated[Session, Depends(get_session)]
